//
//  ViewController.m
//  NSOperation
//
//  Created by izhifei-Devon on 2017/6/8.
//  Copyright © 2017年 izhifei-Devon. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    创建NSInvocationOperation对象
//    NSInvocationOperation * operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(run) object:nil];
//    //开始执行
//    [operation start];
    
//    //创建一个其他队列
//    NSOperationQueue * queue = [[NSOperationQueue alloc]init];
//    //设置为1就是设置为串行队列
//    queue.maxConcurrentOperationCount = 1;
//    
//    //可以添加任务
//    [queue addOperationWithBlock:^{
//        
//    }];
//    //创建NSBlockOperation对象
//    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
//        NSLog(@"%@",[NSThread currentThread]);
//    }];
//    //添加多个Block
//    for (NSInteger i = 0; i < 5; i++) {
//        [blockOperation addExecutionBlock:^{
//            NSLog(@"第%ld次:%@",i,[NSThread currentThread]);
//        }];
//    }
//    //队列添加任务
//    [queue addOperation:blockOperation];
    
    //NSOperation添加依赖
    //任务一
    NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载图片 - %@",[NSThread currentThread]);
        [NSThread sleepForTimeInterval:1.0];
    }];
    
    //任务二
    NSBlockOperation * operation2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"打水印 - %@",[NSThread currentThread]);
         [NSThread sleepForTimeInterval:1.0];
    }];
    
    //任务三
    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"上传图片 - %@",[NSThread currentThread]);
         [NSThread sleepForTimeInterval:1.0];
    }];
    
    //设置依赖
    [operation2 addDependency:operation1];
    [operation3 addDependency:operation2];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    [queue addOperations:@[operation2,operation3,operation1] waitUntilFinished:false];
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
